package com.baizhi.service.ServiceImpl;

import com.baizhi.annotation.LogAnnotation;
import com.baizhi.dao.AtricleDao;
import com.baizhi.entity.Article;
import com.baizhi.lucene.LuceneService;
import com.baizhi.service.AtricleService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * Created by Administrator on 2019/4/7.
 */

@Service
public class AtricleServiceImpl implements AtricleService {
    @Autowired
    private AtricleDao atricleDao;
    @Autowired
    private LuceneService luceneService;



    @Override
    @LogAnnotation("查询所有的文章")
    public Map selectAllArticle(int rows, int page, String articleName) {
        Map map=new HashMap();
        if(articleName!=null){
            if("".equals(articleName)){
                articleName=null;
            }else{
                articleName="%"+articleName+"%";
            }
        }
        int begin=rows*(page-1);
        int size=rows;
        Integer count=atricleDao.selectAllArticleCount(articleName);
        List<Article> articleList=atricleDao.selectAllArticle( begin,size,articleName);

        map.put("total",count);
        map.put("rows",articleList);
        return map;
    }

    @Override
    public Map addArticle(Article article) {
        Map map=new HashMap();
        try {
            atricleDao.addAtricle(article);
            System.out.println("1111111111"+article);
            map.put("isok",true);
        } catch (Exception e) {
            System.out.println("3333333"+article);
            map.put("isok",false);
        }
        return  map;
    }

    @Override
    public Map delateArticle(Integer[] ids) {
        Map map=new HashMap();
        try {
            atricleDao.delateArticle(ids);
            Article article=new Article();
            for (Integer id : ids) {
                article.setArticleId(id);
                luceneService.deleteOne(article);
            }
            map.put("isok",true);
        } catch (Exception e) {
            map.put("isok",false);
        }
        return  map;
    }

    @Override
    public Map updateArticle(Article article) {
        Map map=new HashMap();
        try {
            atricleDao.updateArticle(article);
            map.put("isok",true);
        } catch (Exception e) {
            map.put("isok",false);
        }
        return  map;
    }
}
